package com.eightywork.life.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.eightywork.life.R;
import com.eightywork.life.activity.PmDetailActivity;
import com.eightywork.life.activity.SelectNeighbourActivity;
import com.eightywork.life.imageCache.ImageLoader;
import com.eightywork.life.model.Space;
import com.eightywork.life.service.DataService;

public class SelectNeighbourListAdapter extends ArrayAdapter<Space>{
	private LayoutInflater sInflater;
	private ImageLoader imageLoader;
	private Context scontext;
	private SelectNeighbourActivity activity;
	private int sResource = R.layout.item_select_neighbour;
	public SelectNeighbourListAdapter(Context context, List<Space> list,
			SelectNeighbourActivity activity) {
		super(context, 0, list);
		// TODO Auto-generated constructor stub
		sInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(context);
		this.activity = activity;
		scontext = context;
	}
	@Override
	public View getView (int position, View convertView, final ViewGroup parent) {
		View layout = convertView;
		if (layout == null) {
		    layout = sInflater.inflate( sResource, parent, false);
		}
		final Space  space = getItem(position);
		TextView name = (TextView)layout.findViewById(R.id.authorname);
		name.setText(space.getName());
		ImageView avatar = (ImageView)layout.findViewById(R.id.authorvavatar);
		ImageView verity_type = (ImageView)layout.findViewById(R.id.verify_type);
		verity_type.setBackgroundResource(DataService.getVerifyTypeRes(space.getVerifyType()));
		imageLoader.DisplayImage(space.getAvatar(), avatar, 0, 0, 0);
//		ImageButton select = (ImageButton)layout.findViewById(R.id.selectBtn);
		
//		select.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				Intent intent = new Intent(scontext, PmDetailActivity.class);
//				intent.putExtra("touid", space.getUid());
//				scontext.startActivity(intent);
//			}
//		});
		layout.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(scontext, PmDetailActivity.class);
				intent.putExtra("touid", space.getUid());
				intent.putExtra("toatoavatar", space.getAvatar());
				intent.putExtra("toverify_type", space.getVerifyType());
				scontext.startActivity(intent);
				activity.finish();
				
			}
		});
		return layout;
	}
}
